@extends('backend.layouts.base')
@section('title', '页面管理')
@section('content')
    <link rel="stylesheet" href="/static/css/backend/detail.css">
    <div class="all">
        <div id="app" v-cloak>
            <div class="vue-crumbs">
                <a @click="goParent()">页面管理</a> > 编辑页面
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="15%">
                <div class="vue-head">
                    <div class="vue-main-title">
                        <div class="vue-main-title-left"></div>
                        <div class="vue-main-title-content">基本信息</div>
                    </div>
                    <div class="vue-main-form">
                        <el-form-item label="标题">
                            <el-input v-model="form.title"></el-input>
                        </el-form-item>
                        <el-form-item label="跳转链接">
                            <el-input v-model="form.links"></el-input>
                            <div class="tip">如果填写跳转链接，用户访问这篇文章时直接跳转到填写的跳转链接</div>
                        </el-form-item>
                        <el-form-item label="关键词">
                            <el-input v-model="form.keywords" ></el-input>
                        </el-form-item>
                        <el-form-item label="摘要" prop="description">
                            <el-input v-model="form.description" :rows="5" type="textarea" ref="seo_description"></el-input>
                        </el-form-item>
                        <el-form-item label="发布时间">
                            <el-date-picker
                                    v-model="form.publish_time"
                                    type="datetime"
                                    placeholder="选择发布时间">
                            </el-date-picker>
                            <div class="tip">不填写默认当前时间，发布时间如果超过当前时间，前端将不会显示</div>
                        </el-form-item>
                        <el-form-item label="独立模版">
                            <template>
                                <el-switch
                                        v-model="form.use_tpl"
                                        :active-value="1"
                                        :inactive-value="0"
                                >
                                </el-switch>
                            </template>
                        </el-form-item>
                        <div v-if="form.use_tpl">
                            <el-form-item label="PC端模版">
                                <el-select v-model="form.pc_tpl" clearable placeholder="">
                                    <el-option v-for="(item,index) in pc_detail_tpl_list" :key="index" :label="item.file" :value="item.file"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="手机端模版">
                                <el-select v-model="form.h5_tpl" clearable placeholder="">
                                    <el-option v-for="(item,index) in h5_detail_tpl_list" :key="index" :label="item.file" :value="item.file"></el-option>
                                </el-select>
                            </el-form-item>
                        </div>
                        <el-form-item label="缩略图" prop="thumb">
                            <div class="upload-box" @click="openUpload('thumb')" v-if="!form.thumb">
                                <i class="el-icon-plus" style="font-size:32px"></i>
                            </div>
                            <div @click="openUpload('thumb')" class="upload-boxed" v-if="form.thumb">
                                <img :src="form.thumb" alt="" style="width:150px;height:150px;border-radius: 5px;cursor: pointer;">
                                <div class="upload-boxed-text">点击重新上传</div>
                            </div>
                        </el-form-item>
                        <el-form-item label="发布状态">
                            <template>
                                <el-switch
                                        v-model="form.is_publish"
                                        :active-value="1"
                                        :inactive-value="0"
                                >
                                </el-switch>
                            </template>
                        </el-form-item>
                        <el-form-item label="内容" prop="content">
                            <tinymceee v-model="form.content"></tinymceee>
                        </el-form-item>
                    </div>
                </div>
            </el-form>
            <div class="vue-page">
                <div class="vue-center">
                    <el-button type="primary" @click="submitForm('form')">提交</el-button>
                    <el-button @click="goBack">返回</el-button>
                </div>
            </div>
            <upload-img :upload-show="uploadShow" :name="chooseImgName" @replace="changeProp" @sure="sureImg"></upload-img>
        </div>

    </div>
    <script src="/static/libs/tinymce4.7.5/tinymce.min.js"></script>
    @include('backend.public.tinymceee')
    @include('backend.public.uploadImg')
    <script>
        var app = new Vue({
            el:"#app",
            delimiters: ['[[', ']]'],
            name: 'test',
            data() {
                return{
                    id:0,
                    form:{
                        title:'',
                        keywords:'',
                        description:'',
                        links:'',
                        publish_time:'',
                        use_tpl:'',
                        pc_tpl:'',
                        h5_tpl:'',
                        thumb:'',
                        is_publish:'',
                        content:''
                    },
                    pc_detail_tpl_list:[],
                    h5_detail_tpl_list:[],
                    category_list:[],
                    rules:{
                        title:{ required: true, message: '请输入名称'}
                    },
                    submit_url:'',
                    uploadShow:false,
                    chooseImgName:'',

                }
            },
            created() {

            },
            mounted() {
                this.id = getParam("id");
                if(this.id) {
                    this.is_edit = true;
                    this.submit_url = `{!! absolute_admin_url('content.page.update') !!}`;

                }
                else {
                    this.submit_url = `{!! absolute_admin_url('content.page.create') !!}`;
                }
                this.getData();
            },
            methods: {
                goParent() {
                    window.location.href = `{!! absolute_admin_url('content.page.index') !!}`;
                },
                getData() {
                    let json = {};
                    if(this.id){
                        json['id'] = this.id;
                    }
                    let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                    this.$http.post('{!! absolute_admin_url('content.page.getDetail') !!}',json).then(function (response) {
                            if (response.data.result){
                                this.form = {
                                    ...response.data.data.info,
                                };
                                this.pc_detail_tpl_list=response.data.data.pc_detail_tpl_list;
                                this.h5_detail_tpl_list=response.data.data.h5_detail_tpl_list;
                            }
                            else {
                                this.$message({message: response.data.msg,type: 'error'});
                            }
                            loading.close();
                        },function (response) {
                            this.$message({message: response.data.msg,type: 'error'});
                            loading.close();
                        }
                    );
                },
                submitForm(formName) {
                    let that = this;
                    let json = {
                        title:this.form.title,
                        links:this.form.links,
                        keywords:this.form.keywords,
                        description:this.form.description,
                        publish_time:this.form.publish_time,
                        use_tpl:this.form.use_tpl,
                        pc_tpl:this.form.pc_tpl,
                        h5_tpl:this.form.h5_tpl,
                        source:this.form.source,
                        thumb:this.form.thumb,
                        is_publish:this.form.is_publish,
                        content:this.form.content
                    };
                    if(this.id) {
                        json.id = this.id
                    }console.log('json',json)
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            let loading = this.$loading({target:document.querySelector(".content"),background: 'rgba(0, 0, 0, 0)'});
                            this.$http.post(this.submit_url,{params:json}).then(response => {
                                if (response.data.result) {
                                    console.log('response.data.result',response.data.result)
                                    this.$message({type: 'success',message: '操作成功!'});
                                    this.goParent();
                                } else {
                                    this.$message({message: response.data.msg,type: 'error'});
                                }
                                loading.close();
                            },response => {
                                loading.close();
                            });
                        }
                        else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },

                goBack() {
                    history.go(-1)
                },
                openUpload(str) {
                    this.chooseImgName = str;
                    this.uploadShow = true;
                },
                changeProp(val) {
                    if(val == true) {
                        this.uploadShow = false;
                    }
                    else {
                        this.uploadShow = true;
                    }
                },
                sureImg(name,image,image_url) {
                    this.form[name] = image_url;
                },
                clearImg(str) {
                    this.form[str] = "";
                    this.$forceUpdate();
                },
            },
        })

    </script>
@endsection
